import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    redirect: '/menu-list'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/internal/Login.vue')
  },
  {
    path: '/menu-list',
    name: 'menuList',
    component: () => import('@/views/internal/MenuList.vue')
  },
  {
    path: '/template',
    name: 'template',
    component: () => import('@/layout/template.vue')
  },

  {
    // 系统用户管理
    path: '/internal/sys/user',
    name: 'sysUser',
    component: () => import('@/views/internal/sys/SysUser.vue')
  },

  {
    // SQL翻译器
    path: '/part/vico/sql-translate',
    name: 'VICOSQLTRANSLATE',
    component: () => import('@/views/VICO/SqlTranslate.vue')
  },
  {
    // 账号密码信息
    path: '/part/vico/account-infomation',
    name: 'VICOACCOUNTINFOMATION',
    component: () => import('@/views/VICO/AccountInfomation.vue')
  }

]


const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes
})

const WHITE_PAGE_LIST = ['/login']

// permission check
router.beforeEach((to, from, next) => {
  // 白名单不校验
  if (WHITE_PAGE_LIST.includes(to.path)) {
    next()
  } else {
    // 是否有token
    const userStore = useUserStore()
    if (userStore.sk) {
      next()
    } else {
      ElMessage.error('请登录系统')
      next({ path: '/login' })
    }
  }
})


export default router
